﻿using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Globalization;

[assembly: TagPrefix("Imgalib.Utility", "ucc")]
namespace Imgalib.Utility.Controls
{
    [SupportsEventValidation, ValidationProperty("SelectedValue")]
    [ToolboxData("<{0}:CountryDropDownList runat=server></{0}:CountryDropDownList>")]
    public class CountryDropDownList : DropDownList
    {
        public override void DataBind()
        {
            SortedList slCountry = new SortedList();
            string Key = "";
            string Value = "";

            if (UseFirstItem)
            {
                slCountry.Add(FirstItemText, FirstItemValue);
            }

            foreach (CultureInfo info in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
            {
                RegionInfo info2 = new RegionInfo(info.LCID);
                if (!slCountry.Contains(info2.EnglishName))
                {
                    Value = info2.TwoLetterISORegionName;
                    Key = info2.EnglishName;
                    slCountry.Add(Key, Value);
                }
            }
            base.DataSource = slCountry;
            base.DataTextField = "Key";
            base.DataValueField = "Value";
            base.DataBind();

            if (UseFirstItem)
            {
                base.SelectedIndex = base.Items.IndexOf(base.Items.FindByValue(FirstItemValue));
            }
        }

        private string _firstItemText = "Select Country";
        public string FirstItemText
        {
            get { return _firstItemText; }
            set { _firstItemText = value; }
        }

        private string _firstItemValue = "-1";
        public string FirstItemValue
        {
            get { return this._firstItemValue; }
            set { this._firstItemValue = value; }
        }

        private bool _useFirtItem = false;
        public bool UseFirstItem
        {
            get { return this._useFirtItem; }
            set { this._useFirtItem = value; }
        }
    }
}
